import data from '../../../utils/data';
import util from "../../../utils/utils";
import User from "../../../utils/user";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    platform:1,
    mail_id:0,
    mall_logo:'',
    mall_name:0,
    mall_goods_total:0,
    listData: [],
    listPage: 1,
    listMore: true,
    initUser: true,
  },
  /**
   * 页面载入完成
   */
  initPage: function (options) {
    let channel = User.getShareParam(0);
    if (options.id) {
      options['mall_logo'] = util.getMallLogoText(options['name']);
      this.setData({
        mail_id: options.id,
        platform: options.platform,
        mall_logo: options.mall_logo,
        mall_name: options.name
      });
      this.shareOption['path'] = '/pages/goods/couponShopDetails/couponShopDetails?id=' + options.id + '&ekey=' + channel.ekey + '&platform=' + options.platform + '&name=' + options.name;
    }
    this.initData();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.initData(true);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let self = this;
    if (self.data.listMore === true) {
      data.getMailGoods(self.data.mail_id,self.data.platform,function (result) {
        let list  = self.data.listData;
        console.log('商店店铺商品 result', result);
        let _data = data.formatGoodsList(result.data);
        for (let i = 0; i < _data.length; i++) {
          list.push(_data[i]);
        }
        self.setData({
          listData: list,
          listPage: self.data.listPage + 1
        });
        if (list.length >= result.total) {
          self.setData({
            listMore: false
          });
        }
      }, self.data.listPage + 1, true);
    }
  },
  initData: function (ref) {
    let self = this;
    let update = ref || false;
    wx.showNavigationBarLoading();
    /**
     * 拉取基础数据资源
     */
    data.getMailGoods(self.data.mail_id,self.data.platform,function (result) {
      let _list = data.formatGoodsList(result.data);
      let more = false;
      if (_list.length < result.total) {
        more = true;
      }
      self.setData({
        listData: _list,
        listPage:1,
        mall_goods_total:result.total,
        listMore:more
      });
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1, update);
  }
});
